from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from blog.models import Article, Comment
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.core.urlresolvers import reverse
from django import forms
# Create your views here.


class FileUploadForm(forms.Form):
    file_source = forms.FileField()


# 博客主界面
def home(request):
    postList = Article.objects.all()
    # 根据文章数目进行分页
    paginator = Paginator(postList, 2)
    page = request.GET.get('page')
    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        contacts = paginator.page(1)
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)

    return render(request, r'blog/home.html', {'postList': postList, "contactsList": contacts})


# 博客文章,根据id获取
def detail(request, postId):
    post = get_object_or_404(Article, pk=postId)

    # 获取评论：
    postComments = post.comment_set.all()
#     comments = None
#     if postComments:
#         comments = _commentAndReply(postComments)
    comments = postComments
    # 这里主要是要列出所有的文章,用于导航的
    postList = Article.objects.all()

    return render(request, r'blog/post.html', {'post': post, 'postList': postList, 'comments': comments})


# # 处理上传的文件
# def handle_uploaded_file(request):
#     print('upload form')
#     if request.method == 'POST':
#         form = FileUploadForm(data=request.POST, files=request.FILES)
# #         handle_write_file(form)
#         if form.is_valid():
#             print('valid form')
#         else:
#             print('invalid form')
#             print(form.errors)
#         return HttpResponse("complete")
#     form = FileUploadForm(data=request.POST, files=request.FILES)
#     return render(request, "blog/testUpload.html", {'form': form})
# #     return HttpResponse("uploaded")
#
#
# def handle_write_file(f):
#     with open(r'e:/name.jpg', 'wb+') as destination:
#         for chunk in f.chunks():
#             destination.write(chunk)


# 回复和回复他人的内容绑定在一个元组
# def _commentAndReply(comments):
#     for comment in comments:
#         yield(comment, comment.commentreply_set.all())


# 提交评论
def comment(request):
    if request.POST:
        # 添加相关评论到文章
        postid = request.POST['postid']
        nickName = request.POST['nickname']
        content = request.POST['comment']

        if not nickName or nickName.isspace():
            nickName = "游客"

        comment = Comment.objects.create_comment(
            postid=postid, nickName=nickName, content=content)

        return HttpResponseRedirect(reverse('blog:detail', args=(postid,)))
    return HttpResponse("提交错误")


# 回复他人评论
# def commentReply(request):
#     nickName = request.POST['nickName']
#     commentId = request.POST['commentId']
#     replyContent = request.POST['replyContent']
#
#     if not nickName or nickName.isspace():
#         nickName = "游客"
#
#     commentReply = CommentReply.objects.crete_commentreply(
#         commentId=commentId, reply=replyContent, nickName=nickName)
#     return HttpResponse("提交错误 {}".format(commentId))


#  获得相关postId文章的全部评论
def updateComment(request, postId):
    post = get_object_or_404(Article, pk=postId)
    comments = post.comment_set.all()
    return HttpResponse("update {}" .format(comments))
